<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\Admin\Role;


class AdminUserController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $data = Admin::withTrashed()->with('role')->paginate($this->pagesize);
        return view('admin.admin_user.index',compact('data'));

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $roles = Role::pluck('name','id')->toArray();
//        dd($roles);
        return view('admin.admin_user.add',compact('roles'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
     //验证后期补充
        ]);
        $objAdmin = Admin::create($request->except(['_token','repass']));

        if($objAdmin){
            return ['status'=>200,'msg'=>'成功'];
        }else{
            return ['status'=>-1,'msg'=>'失败'];
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $roles = Role::pluck('name','id')->toArray();
        $data = Admin::find($id);
        return view('admin.admin_user.edit',compact(['data','roles']));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $model = Admin::find($id);
//        $spass = $request->input('password');

//        $bool = Hash::check($spass,$model->password);
//        if($bool){
            $res = $model->update($request->except('_token','repass'));
            if($res){
                return ['status'=>200,'msg'=>'成功'];
            }
//        }
        return ['status'=>-1,'msg'=>'失败'];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        Admin::find($id)->delete();  //启用软删除
        //强制删除 在配置了软删除的时候，真实的删除
//        User::find($id)->forceDelete();
        return ['status'=>200,'msg'=>'删除成功'];
    }
}
